package pkg.drowne.org;

import java.security.NoSuchAlgorithmException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;

public class register extends Activity implements OnClickListener, Runnable {

	private ImageButton registerButton, resetButton;
	private EditText editUsername, editPassword, 
			editPasswordConfirmation, editEmail, editNickname;
	private String username, password, hashedPassword, email, nickname;
	private ProgressDialog pd;
	
	 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
	        WindowManager.LayoutParams.FLAG_FULLSCREEN,
	        WindowManager.LayoutParams.FLAG_FULLSCREEN
        );
        
        // creates the activity
        setContentView(R.layout.register);
        
        // Views
        registerViews();
        setupOnClickListeners();
    }
    
    private void registerViews() {

    	registerButton = (ImageButton) this.findViewById(R.id.btnRegister);
    	resetButton = (ImageButton) this.findViewById(R.id.btnReset);
    	editUsername = (EditText) this.findViewById(R.id.edtUsername);
    	editNickname = (EditText) this.findViewById(R.id.edtNickname);
    	editPassword = (EditText) this.findViewById(R.id.edtPassword);
    	editPasswordConfirmation = (EditText) this.findViewById(R.id.edtConfirmPassword);
    	editEmail = (EditText) this.findViewById(R.id.edtEmail);
	
    }
    
	public void setupOnClickListeners() {
    	
        registerButton.setOnClickListener(this);
        resetButton.setOnClickListener(this);
    	
    }
	
	public void onClick(View v) {
		
		switch(v.getId()) {
			
		case R.id.btnRegister:
			// register button pressed
			// validate the password locally
			
			
			try {
				fillUserAndPass();
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			}
			

			pd = ProgressDialog.show(this, "Registering...", "Please wait...", true, false);
        	Thread cThread = new Thread(this);
        	cThread.start();

			break;

		case R.id.btnReset:
			// reset button pressed
			// so reset each text field
			editUsername.setText("");
			editEmail.setText("");
			editNickname.setText("");
			editPasswordConfirmation.setText("");
			editPassword.setText("");
			break;
				
		}
		
	}

	private void fillUserAndPass() throws NoSuchAlgorithmException {
		
		username = editUsername.getText().toString();
		password = editPassword.getText().toString();
		hashedPassword = Utils.md5Hash(password);
		email = editEmail.getText().toString();
		nickname = editNickname.getText().toString();
		
	}

	@Override
	public void run() {

		boolean answer = Connection.register(username, hashedPassword, nickname, email);
		
		if(answer) {
			// Registered Successfully
			this.finish();
			handler.sendEmptyMessage(1);
			
			startActivity(new Intent(getBaseContext(), NewGameMenu.class));
			
		} else {
			// wrong login
			handler.sendEmptyMessage(0);
		}
		
	}
	
	private final Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			pd.dismiss();
			if (msg.what == 1) {
				Utils.showToastShort(getBaseContext(), "User Registered");
			} else {
				Utils.showToastShort(getBaseContext(), "We are sorry, an error occurred while registering you...");
			}

		}
	};
	
}
